package utils

import (
	"encoding/hex"
	"errors"
	"fmt"
	"strconv"
	"water-temperature-server/models"
)

// GetDeviceHexAdder ChannelNo 通道号(ChannelNo，3位数字长,范围为 001-032)
// SensorIndex 在通道中的顺序(2位数字长，范围为01-50)
// 传感器的Modbus地址 = (ChannelNo-1)*100 + (SensorIndex-1)*2
func GetDeviceHexAdder(ChannelNo int, SensorIndex int) string {
	decimalNumber := (ChannelNo-1)*100 + (SensorIndex-1)*2
	return fmt.Sprintf("%04X", decimalNumber)
}

// DecimalToHex
// 十进制转十六进制
func DecimalToHex(decimalData int) string {
	return strconv.FormatInt(int64(decimalData), 16)
}

// HexToDecimal 十六进制转十进制
func HexToDecimal(hexData string) int64 {
	decimalFromHex, err := strconv.ParseInt(hexData, 16, 0)
	if err != nil {
		fmt.Println("十六进制转十进制 错误:", err)
		return -1
	}
	return decimalFromHex
}

// ReadDeviceData ChannelNo 通道号(ChannelNo，3位数字长,范围为 001-032)
// DeviceAdder 设备地址
func ReadDeviceData(ChannelNo int, SensorIndex int, DeviceAdder int, DeviceNum int) ([]byte, error) {
	preHexData := "0000000000"
	DeviceAdderHex := fmt.Sprintf("%02X", DeviceAdder)
	DeviceStartAdder := GetDeviceHexAdder(ChannelNo, SensorIndex)
	DataAdder := fmt.Sprintf("%04X", DeviceNum*2)
	data, err := hex.DecodeString(fmt.Sprintf("%v06%v03%v%v", preHexData, DeviceAdderHex, DeviceStartAdder, DataAdder))
	if err != nil {
		return []byte{}, errors.New("转换错误:" + err.Error())
	}
	return data, nil
}

func ParseData(data string, ChannelNo int, SensorIndex int, CreateTime string) *[]models.WaterTemperaTure {
	ChannelNoStr := fmt.Sprintf("%03d", ChannelNo)
	//byteLen := data[5*2 : 5*2+2]
	//deviceAdder, _ := strconv.Atoi(data[6*2 : 6*2+2])
	//funCode := data[7*2 : 7*2+2]

	dataByteLen := data[8*2 : 8*2+2]
	dataList := []models.WaterTemperaTure{}
	if len(data) <= 32 {
		return &dataList
	}
	dataHex := data[8*2+2 : 8*2+2+HexToDecimal(dataByteLen)*2]
	dateLen := HexToDecimal(dataByteLen) / 4

	for i := 0; i < int(dateLen); i++ {
		item := models.WaterTemperaTure{}
		itemHexData := float64(HexToDecimal(dataHex[i*4*2:(i+1)*4*2])) / 10000
		item.DataValue = itemHexData
		item.ChannelNo = ChannelNoStr
		item.SensorIndex = fmt.Sprintf("%02d", SensorIndex)
		item.CreateTime = CreateTime
		item.DeviceId = fmt.Sprintf("%v%v", ChannelNoStr, item.SensorIndex)
		item.ProId = "105"
		if ChannelNo == 14 {
			item.ChannelName = "截鱼系统"
		} else if ChannelNo == 13 {
			item.ChannelName = "尾水处"
		} else if ChannelNo == 12 {
			item.ChannelName = "左岸取水口4下"
		} else if ChannelNo == 11 {
			item.ChannelName = "左岸取水口4中"
		} else if ChannelNo == 10 {
			item.ChannelName = "左岸取水口4上"
		} else if ChannelNo == 9 {
			item.ChannelName = "左岸取水口3下"
		} else if ChannelNo == 8 {
			item.ChannelName = "左岸取水口3中"
		} else if ChannelNo == 7 {
			item.ChannelName = "左岸取水口3上"
		} else if ChannelNo == 6 {
			item.ChannelName = "右岸取水口2下"
		} else if ChannelNo == 5 {
			item.ChannelName = "右岸取水口2中"
		} else if ChannelNo == 4 {
			item.ChannelName = "右岸取水口2上"
		} else if ChannelNo == 3 {
			item.ChannelName = "右岸取水口1下"
		} else if ChannelNo == 2 {
			item.ChannelName = "右岸取水口1中"
		} else if ChannelNo == 1 {
			item.ChannelName = "右岸取水口1上"
		}
		SensorIndex = SensorIndex + 1
		dataList = append(dataList, item)
	}

	return &dataList
}
